var Api = require("../Api.js");
var sign = require("sign.js")
/**
 * url : 请求地址的名称 与 api.js对应 
 * that  一个Page对象
 * cb  成功后交给数据装填
 */
function MyGet(url, that, cb, data) {
    wx.request({
        url: Api.Url(url),
        data: data,
        method: 'GET',
        success: function (res) {
            typeof cb === 'function' && cb(res, that);
        }
    })
}

function MyGetWithToast(url, that, cb, data) {
    wx.showToast({ title: '加载中', icon: 'loading', duration: 10000 })
    wx.request({
        url: Api.Url(url),
        data: data,
        method: 'GET',
        success: function (res) {
            if (res.data.success) {
                typeof cb === 'function' && cb(res, that);
                wx.hideToast();
                 
            } else {
                wx.showToast({ title: res.data.msg, icon: 'loading', duration: 2000 })
            }

        }
    })
}


 
/**
 * 下拉刷新用Get方法
 */
function MyGetWithRefresh(url, that, cb, data) {
    wx.showToast({ title: '加载中', icon: 'loading', duration: 10000 })
    wx.request({
        data: data,
        url: Api.Url(url),
        method: 'GET',
        success: function (res) {
            if (res.data.success) {
                typeof cb === 'function' && cb(res, that);
                    wx.hideToast();
            } else {
                wx.showToast({ title: res.data.msg, icon: 'loading', duration: 2000 })
            }
        },
        fail: function () {
        },
        complete: function () {
            wx.stopPullDownRefresh();
        }
    })
}

/**
 * 提交方法
 */
function MyPost(url, that, cb, data) {
    wx.showToast({ title: '加载中', icon: 'loading', duration: 10000 })
    wx.request({
        url: Api.Url(url),
        method: 'POST',
        success: function (res) {
            typeof cb === 'function' && cb(res, that);
        },
        complete: function () {
            wx.hideToast();
        }
    })
}



function MyPostWithToast(url, that, cb, data) {
    wx.showToast({ title: '加载中', icon: 'loading', duration: 10000 })
    wx.request({
        url: Api.Url(url),
        data: data,
        header: {'content-type': 'application/x-www-form-urlencoded'},
        method: 'POST',
        success: function (res) {
            if (res.data.success) {
                typeof cb === 'function' && cb(res, that);
                wx.showToast({ title: res.data.msg, icon: 'success', duration: 2000 })
            } else {
                wx.showToast({ title: res.data.msg, icon: 'loading', duration: 2000 })
            }

        }
    })
}




module.exports = {
    MyGet: MyGet,
    MyPost: MyPost,
    MyGetWithRefresh: MyGetWithRefresh,
    MyGetWithToast: MyGetWithToast,
    MyPostWithToast:MyPostWithToast,


}